package cn.iocoder.yudao.module.trade.service.ordersafetycheckitemrelation;

import cn.iocoder.yudao.framework.common.util.CollectionUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.trade.dal.dataobject.ordersafetychecktemplateitem.OrderSafetyCheckTemplateItemDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.ordersafetycheckterm.OrderSafetyCheckTermDO;
import cn.iocoder.yudao.module.trade.dal.mysql.ordersafetychecktemplateitem.OrderSafetyCheckTemplateItemMapper;
import cn.iocoder.yudao.module.trade.dal.mysql.ordersafetycheckterm.OrderSafetyCheckTermMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.trade.controller.admin.ordersafetycheckitemrelation.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.ordersafetycheckitemrelation.OrderSafetyCheckItemRelationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.trade.dal.mysql.ordersafetycheckitemrelation.OrderSafetyCheckItemRelationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;

/**
 * 安检单与安检明细关联 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class OrderSafetyCheckItemRelationServiceImpl implements OrderSafetyCheckItemRelationService {

    @Resource
    private OrderSafetyCheckItemRelationMapper orderSafetyCheckItemRelationMapper;

    @Resource
    private OrderSafetyCheckTermMapper orderSafetyCheckTermMapper;

    @Resource
    private OrderSafetyCheckTemplateItemMapper orderSafetyCheckTemplateItemMapper;
    @Override
    public Long createOrderSafetyCheckItemRelation(OrderSafetyCheckItemRelationSaveReqVO createReqVO) {
        // 插入
        OrderSafetyCheckItemRelationDO orderSafetyCheckItemRelation = BeanUtils.toBean(createReqVO, OrderSafetyCheckItemRelationDO.class);
        orderSafetyCheckItemRelationMapper.insert(orderSafetyCheckItemRelation);
        // 返回
        return orderSafetyCheckItemRelation.getId();
    }

    @Override
    public void updateOrderSafetyCheckItemRelation(OrderSafetyCheckItemRelationSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderSafetyCheckItemRelationExists(updateReqVO.getId());
        // 更新
        OrderSafetyCheckItemRelationDO updateObj = BeanUtils.toBean(updateReqVO, OrderSafetyCheckItemRelationDO.class);
        orderSafetyCheckItemRelationMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderSafetyCheckItemRelation(Long id) {
        // 校验存在
        validateOrderSafetyCheckItemRelationExists(id);
        // 删除
        orderSafetyCheckItemRelationMapper.deleteById(id);
    }

    private void validateOrderSafetyCheckItemRelationExists(Long id) {
        if (orderSafetyCheckItemRelationMapper.selectById(id) == null) {
            throw exception(ORDER_SAFETY_CHECK_ITEM_RELATION_NOT_EXISTS);
        }
    }

    @Override
    public OrderSafetyCheckItemRelationDO getOrderSafetyCheckItemRelation(Long id) {
        return orderSafetyCheckItemRelationMapper.selectById(id);
    }

    @Override
    public PageResult<OrderSafetyCheckItemRelationDO> getOrderSafetyCheckItemRelationPage(OrderSafetyCheckItemRelationPageReqVO pageReqVO) {
        return orderSafetyCheckItemRelationMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveCheckItemRelationBath(List<OrderSafetyCheckItemRelationRespVO> relationRespVOList) {
        orderSafetyCheckItemRelationMapper.insertBatch(BeanUtils.toBean(relationRespVOList, OrderSafetyCheckItemRelationDO.class));
    }

    /**
     *  查看安检单
     * @param id
     * @return
     */
    @Override
    public List<OrderSafetyCheckItemRelationRespVO> getCheckItemRelationByCheckId(Long id) {
        LambdaQueryWrapperX<OrderSafetyCheckItemRelationDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eqIfPresent(OrderSafetyCheckItemRelationDO::getCheckId, id);
        List<OrderSafetyCheckItemRelationRespVO> respVOList = BeanUtils.toBean(orderSafetyCheckItemRelationMapper.selectList(wrapperX), OrderSafetyCheckItemRelationRespVO.class);
        List<OrderSafetyCheckItemRelationRespVO> rlist1 = new ArrayList<>();
        if (!CollectionUtil.isEmpty(respVOList)) {

            //获取安检项
            List<Long> collect = respVOList.stream().map(OrderSafetyCheckItemRelationRespVO::getCheckItemId).distinct().collect(Collectors.toList());

            LambdaQueryWrapperX<OrderSafetyCheckTemplateItemDO> x1 = new LambdaQueryWrapperX<>();
            x1.inIfPresent(OrderSafetyCheckTemplateItemDO::getId, collect);
            List<OrderSafetyCheckTemplateItemDO> doList = orderSafetyCheckTemplateItemMapper.selectList(x1);
            doList.forEach(y->{

                OrderSafetyCheckItemRelationRespVO respVO = new OrderSafetyCheckItemRelationRespVO();
                respVO.setId(id);
                respVO.setCheckId(id);
                respVO.setCheckItemId(y.getId());
                respVO.setOption(y.getTitle());
                respVO.setCheckItemType(respVOList.stream().filter(x -> x.getCheckItemId().equals(y.getId())).findFirst().get().getCheckItemType());
                rlist1.add(respVO);
            });

            rlist1.forEach(x -> {
                List<OrderSafetyCheckItemRelationRespVO> voList = new ArrayList<>();

                LambdaQueryWrapperX<OrderSafetyCheckTermDO> x2 = new LambdaQueryWrapperX<>();
                x2.eqIfPresent(OrderSafetyCheckTermDO::getItemId, x.getCheckItemId());
                List<OrderSafetyCheckTermDO> list = orderSafetyCheckTermMapper.selectList(x2);


                list.forEach(li -> {
                    OrderSafetyCheckItemRelationRespVO respVO = new OrderSafetyCheckItemRelationRespVO();
                    respVO.setId(id);
                    respVO.setChenkItemTremId(li.getId());
                    respVO.setOption(li.getOptions());
                    respVO.setCheckItemType(li.getType());
                    if (li.getType() == 5 || li.getType() == 6 || li.getType() == 3) {
                        Optional<OrderSafetyCheckItemRelationRespVO> first = respVOList.stream().filter(res -> res.getChenkItemTremId().equals(li.getId())).findFirst();
                        if (first.isPresent()) {
                            respVO.setContent(first.get().getContent());
                        }

                    } else {
                        Optional<OrderSafetyCheckItemRelationRespVO> first = respVOList.stream().filter(res -> res.getChenkItemTremId().equals(li.getId())).findFirst();
                        if (first.isPresent()) {
                            respVO.setOptions(first.get().getOptions());
                        }
                        respVO.setContent(x.getContent());
                    }

                    voList.add(respVO);
                });
                x.setRelationList(voList);
            });

        }
        return rlist1;
    }


}